#include	"PlayerTank.h"
#include	<Math/Vector.h>
#include	<windows.h>
#include	<gl/gl.h>
#include	<gl/glu.h>

PlayerTank::PlayerTank()
	:	ITank()
{
	m_color.Set(0.5f, 1.0f, 1.0f, 1.0f);
}

void PlayerTank::update(double delay)
{
	if((m_pos == Vector2i(-1, -1)) && (m_life > 0))
	{
		m_pos = m_map->getPlayerPos();
	}
	else if(m_life <= 0)
	{
		return;
	}
	// space
	m_lastShot += float(delay);
	if((::GetAsyncKeyState(VK_SPACE) & 0x8000) != 0)
	{
		shoot();
	}
	if((-EPSILON <= m_distance) && (m_distance <= EPSILON))
	{
		// left arrow
		if((::GetAsyncKeyState(VK_LEFT) & 0x8000) != 0)
		{
			m_facing = left;
			if(!checkWall(left))
			{
				m_distance = float(m_speed * delay);				
			}
		}
		// up arrow
		if(((::GetAsyncKeyState(VK_UP) & 0x8000) != 0) &&
			((-EPSILON <= m_distance) && (m_distance <= EPSILON)))
		{
			m_facing = up;
			if(!checkWall(up))
			{
				m_distance = float(m_speed * delay);				
			}
		}
		// right arrow
		if(((::GetAsyncKeyState(VK_RIGHT) & 0x8000) != 0) &&
			((-EPSILON <= m_distance) && (m_distance <= EPSILON)))
		{
			m_facing = right;
			if(!checkWall(right))
			{
				m_distance = float(m_speed * delay);				
			}
		}
		// down arrow
		if(((::GetAsyncKeyState(VK_DOWN) & 0x8000) != 0) &&
			((-EPSILON <= m_distance) && (m_distance <= EPSILON)))
		{
			m_facing = down;
			if(!checkWall(down))
			{
				m_distance = float(m_speed * delay);				
			}
		}
	}
	else
	{
		m_distance += float(m_speed * delay);
	}
	if(m_distance >= (1.0f - EPSILON))
	{
		switch(m_facing)
		{
		case left:
			m_pos[0] -= 1;
			break;
		case right:
			m_pos[0] += 1;
			break;
		case up:
			m_pos[1] -= 1;
			break;
		case down:
			m_pos[1] += 1;
			break;
		}
		m_distance = 0.0f;
	}
}

bool PlayerTank::isPlayer() const
{
	return true;
}

